package repositories

import (
	"gitee.com/cjf8134/bbs_serve/internal/pkg/models"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/requests"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/utils/constutil"
)

// CategoryList  分类列表
func (r *MysqlApiServeRepository) CategoryList(req *requests.CategoryList) (categoryList []*models.Category, err error) {
	model := r.db.Model(models.Category{})
	if req.PID > 0 {
		model = model.Where("pid = ?", req.PID)
	}
	if req.Name != "" {
		model = model.Where("name LIKE %(`?`)%", req.Name)
	}

	if req.Limit <= 0 {
		req.Limit = constutil.DefaultPageSize
	}

	if req.Limit > constutil.DefaultMaxPageSize {
		req.Limit = constutil.DefaultMaxPageSize
	}

	if req.Page <= 0 {
		req.Page = 0
	}
	err = model.Offset(req.Page * req.Limit).Order("`rank` desc").Limit(req.Limit).Find(&categoryList).Error
	return
}
